//
//  Player.m
//  OurRelaxDemo
//
//  Created by 于宾尧 on 15/6/30.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "Player.h"

#import "MusicDetailShareHandle.h"

@interface Player ()
@end

@implementation Player

+(instancetype)shareDataHandle
{
    static Player *playing = nil;
    static dispatch_once_t oneTaken;
    dispatch_once(&oneTaken, ^{
        
        playing = [[Player alloc]init];
        playing.player = [[STKAudioPlayer alloc] init];
        playing.playingIndex = 0;
        //playing.currentUrlArray = [NSMutableArray array];
        playing.playUrlArray = [NSMutableArray array];
        playing.urlArray = [NSMutableArray array];
        
    });
    
    return playing;
}

+(void)Play:(NSMutableArray *)playUrlArray
{
#pragma 改
    if ([Player shareDataHandle].currentUrlArray.count == 0) {
        
        return;
    }
    [TagHandle shareTagHandle].isPlaying = YES;
    
    [Player shareDataHandle].playUrlArray = [NSMutableArray arrayWithArray:playUrlArray];
    
    NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex];
    //[Player shareDataHandle].urlPlayStr = playUrl;
    NSURL *url = [NSURL URLWithString:playUrl];
    
    [Player shareDataHandle].player.volume = 1.0f;
    [[Player shareDataHandle].player playURL:url];
    
}

+(void)nextPlayRadio
{
#pragma 改
    if ([Player shareDataHandle].currentUrlArray.count == 0) {
        
        return;
    }
    [Player shareDataHandle].playingIndex ++;
    
    if ([Player shareDataHandle].playingIndex == [Player shareDataHandle].playUrlArray.count) {
        
        [Player shareDataHandle].playingIndex = 0;
        
    }
    NSLog(@"index = %ld",[Player shareDataHandle].playingIndex);
    
    //[MusicDetailShareHandle shareListDataHandle].statusMusic = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:self.playingIndex];
    
    [self Play:[Player shareDataHandle].playUrlArray];
    NSLog(@"%@",[MusicDetailShareHandle shareListDataHandle].statusMusic.song_name);
    
}

-(void)PlayForArray
{
#pragma 改
    if ([Player shareDataHandle].currentUrlArray.count == 0) {
        
        return;
    }
    [TagHandle shareTagHandle].isPlaying = YES;
    
    NSString *playURL = [[Player shareDataHandle].currentUrlArray objectAtIndex:self.playingIndex];
    //NSLog(@"playUrl = %@",playURL);
    NSURL *urlPlaying = [NSURL URLWithString:playURL];
    
#pragma 改 ordIndex
    self.oldIndex = self.playingIndex;
    
    self.player.volume = 1.0f;
    [self.player playURL:urlPlaying];
        
    [MusicDetailShareHandle shareListDataHandle].statusMusic = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:self.playingIndex];
}

-(void)pausePlay
{
    [[Player shareDataHandle].player pause];
    [TagHandle shareTagHandle].isPlaying = NO;
}

-(void)NextPlay
{
#pragma 改
    if ([Player shareDataHandle].currentUrlArray.count == 0) {
        
        return;
    }
    self.playingIndex ++;
    
    if (self.playingIndex == self.currentUrlArray.count) {
        
        self.playingIndex = 0;
        
    }
    NSLog(@"index = %ld",[Player shareDataHandle].playingIndex);
    
    //[MusicDetailShareHandle shareListDataHandle].statusMusic = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:self.playingIndex];
    if ([TagHandle shareTagHandle].isPlaying) {
        
        [self PlayForArray];
    } else {
    
        [MusicDetailShareHandle shareListDataHandle].statusMusic = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:self.playingIndex];
        return;
    }
    NSLog(@"%@",[MusicDetailShareHandle shareListDataHandle].statusMusic.song_name);
}

-(void)lastPlay
{
    //[[Player shareDataHandle].player stop];
    //[[Player shareDataHandle].player release];
#pragma 改
    if ([Player shareDataHandle].currentUrlArray.count == 0) {
        
        return;
    }
    
    self.playingIndex --;
    
    if (self.playingIndex < 0) {
        
        self.playingIndex = 0;
    }
    
    //[MusicDetailShareHandle shareListDataHandle].statusMusic = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:self.playingIndex];
    if ([TagHandle shareTagHandle].isPlaying) {
        
        [self PlayForArray];
        
    } else {
    
        [MusicDetailShareHandle shareListDataHandle].statusMusic = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:self.playingIndex];
        return;
    }
    NSLog(@"%@",[MusicDetailShareHandle shareListDataHandle].statusMusic.song_name);

}

// *************************************
-(void)randomPlay
{

    if ([Player shareDataHandle].player.stopReason == STKAudioPlayerStopReasonPendingNext) {
        
    
        [Player shareDataHandle].playingIndex = arc4random()%([Player shareDataHandle].playUrlArray.count - 1);
    }
    NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex];
    
    NSURL *url = [NSURL URLWithString:playUrl];
    
    [Player shareDataHandle].player = [[STKAudioPlayer alloc]init];
    
    [Player shareDataHandle].player.volume = 1.0f;
    
    [[Player shareDataHandle].player playURL:url];
    
}

-(void)orderPlay
{
    
}

-(void)roundPlay
{
    if ([Player shareDataHandle].player.stopReason == STKAudioPlayerStopReasonPendingNext) {
        
        NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex];
        
        NSURL *url = [NSURL URLWithString:playUrl];
        
        [Player shareDataHandle].player = [[STKAudioPlayer alloc] init];
        
        [Player shareDataHandle].player.volume = 1.0f;
        [[Player shareDataHandle].player playURL:url];
    }
 
}

@end
